package com.ruoyi.book.service.impl;

import java.util.Date;
import java.util.List;

import cn.hutool.crypto.SecureUtil;
import com.ruoyi.book.domain.Book;
import com.ruoyi.book.mapper.BookMapper;
import com.ruoyi.book.mapper.BorrowMapper;
import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.book.mapper.BookreturnMapper;
import com.ruoyi.book.domain.Bookreturn;
import com.ruoyi.book.service.IBookreturnService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 还书管理Service业务层处理
 *
 * @author ruoyi
 * @date 2022-12-30
 */
@Service
public class BookreturnServiceImpl implements IBookreturnService
{
    @Autowired
    private BookreturnMapper bookreturnMapper;

    @Autowired
    private BorrowMapper borrowMapper;

    @Autowired
    private BookMapper bookMapper;

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询还书管理
     *
     * @param id 还书管理主键
     * @return 还书管理
     */
    @Override
    public Bookreturn selectBookreturnById(Long id)
    {
        return bookreturnMapper.selectBookreturnById(id);
    }

    /**
     * 查询还书管理列表
     *
     * @param bookreturn 还书管理
     * @return 还书管理
     */
    @Override
    public List<Bookreturn> selectBookreturnList(Bookreturn bookreturn)
    {
        return bookreturnMapper.selectBookreturnList(bookreturn);
    }

    /**
     * 新增还书管理
     *
     * @param bookreturn 还书管理
     * @return 结果
     */
    @Override
    @Transactional
    public int insertBookreturn(Bookreturn bookreturn)
    {
        //删除借阅记录
        int i = borrowMapper.deleteByBooKId(bookreturn);
        if(i==0){
            throw new ServiceException("业务异常");
        }
        //添加归还记录
        bookreturn.setCreateTime(DateUtils.getNowDate());
        int i2 = bookreturnMapper.insertBookreturn(bookreturn);
        if(i2==0){
            throw new ServiceException("业务异常");
        }

        //更新书本数量
        Book book = bookMapper.selectBookById(bookreturn.getBookId());
        book.setCount(book.getCount()+1);
        int i1 = bookMapper.updateBook(book);
        if(i1==0){
            throw new ServiceException("业务异常");
        }

        //判断是否逾期归还进行用户锁定操作
        Date nowDate = DateUtils.getNowDate();
        Date returnDate = bookreturn.getReturnDate();
        if(nowDate.after(returnDate)){
            //当前时间是归还时间之后
            SysUser user = SecurityUtils.getLoginUser().getUser();
            if (StringUtils.isNotNull(user.getUserId()) && user.isAdmin())
            {
                throw new ServiceException("不允许操作超级管理员用户");
            }
            user.setStatus("1");
            userMapper.updateUser(user);

            //强制下线
            String tokenId = SecurityUtils.getLoginUser().getToken();
            redisCache.deleteObject(CacheConstants.LOGIN_TOKEN_KEY + tokenId);
        }


        return 1;
    }

    /**
     * 修改还书管理
     *
     * @param bookreturn 还书管理
     * @return 结果
     */
    @Override
    public int updateBookreturn(Bookreturn bookreturn)
    {
        bookreturn.setUpdateTime(DateUtils.getNowDate());
        return bookreturnMapper.updateBookreturn(bookreturn);
    }

    /**
     * 批量删除还书管理
     *
     * @param ids 需要删除的还书管理主键
     * @return 结果
     */
    @Override
    public int deleteBookreturnByIds(Long[] ids)
    {
        return bookreturnMapper.deleteBookreturnByIds(ids);
    }

    /**
     * 删除还书管理信息
     *
     * @param id 还书管理主键
     * @return 结果
     */
    @Override
    public int deleteBookreturnById(Long id)
    {
        return bookreturnMapper.deleteBookreturnById(id);
    }

    @Override
    public List<Bookreturn> getListByUserId(Bookreturn bookreturn) {
        Long userId = SecurityUtils.getUserId();
        bookreturn.setUserId(userId);
        return bookreturnMapper.selectBookreturnList(bookreturn);
    }
}
